package top.drinyeit.weilong.presenter;

import android.content.Intent;
import android.location.Location;
import android.util.Log;

import top.drinyeit.weilong.activity.AddNewAddressActivity;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.LocationUtils;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by bigHead on 2018/1/4.
 */

public class AddNewAddressPresenter {
    private AddNewAddressActivity activity;
    String longitude = "";
    String latitude = "";

    public AddNewAddressPresenter(AddNewAddressActivity activity){
        this.activity = activity;
    }

    //添加新地址
    public void addNewAdd(String consignee_name, String consignee_phone, String address_detail){
        activity.showLoading();
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        Location location = LocationUtils.getInstance(activity).showLocation();
        if (location == null){
//            activity.showToastMsg("获取地理位置失败,请检查网络/GPS定位后重试");
            longitude = "";
            latitude = "";
        }else {
            String address = "纬度：" + location.getLatitude() + "经度：" + location.getLongitude();
            Log.d( "FLY.LocationUtils", address );
            longitude = location.getLongitude() + "";
            latitude = location.getLatitude() + "";
        }
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS){
                    activity.setResult(1);
                    activity.finish();
                }
            }
        });
        RetrofitMethod.getInstance().addNewAdd(subscriber,user_id,consignee_name,consignee_phone,address_detail,longitude,latitude);
    }
}
